<div class="goods-dialog">
    <div class="flex flex-pack-end mb-10">
        <div class="input-group search-input-group pull-right">
            <input type="text" class="form-control" name="name" placeholder="名称">
            <span class="input-group-btn"><button class="btn btn-primary search">搜索</button></span>
        </div>
    </div>
    <div class="dialog-box tab-content">
        <table class="table v-table">
            <thead>
            <tr>
                <th class="text-center">{$title}</th>
            </tr>
            </thead>
            <tbody id="list">
            </tbody>
        </table>
        <div class="page clearfix">
            <div class="M-box3 m-style fr">
            </div>
        </div>
    </div>
</div>
<script id='tpl_list' type='text/html'>
<%if data!=''%>
    <%each data as item index%>
    <tr>
        <td class="text-left">
            <label class="checkbox-inline">
                <input class="decorate" type="checkbox" data-id="<%item[id]%>" data-name="<%item[name]%>"
                <%if inArray(item[id], selected_id_array)%>checked<%/if%>> <%item[name]%>
            </label>
        </td>
    </tr>
    <%/each%>
<%else %>
    <tr>
        <td class="text-left" style="text-align: center;">
            <label>请添加<%if name=='coupon_name'%>优惠券<%else if name=='giftvoucher_name'%>礼品券<%/if%></label>
        </td>
    </tr>
<%/if%>
</script>
<script>
    require(['utilAdmin', 'tpl'], function (utilAdmin, tpl) {
        utilAdmin.initPage(LoadingInfo)

        tpl.helper("inArray", function (item, array) {
            return ($.inArray(item, array) === -1) ? false : true;
        });

        function LoadingInfo(page_index) {
            var val = $('input[name="name"]').val();
            $.ajax({
                type: "post",
                url: "{$listUrl}",
                async: true,
                data: {
                    "page_index": page_index,
                    "search_text": val,
                    "not_expired": true,
                },
                success: function (data) {
                    var type = '{$type}'
                    if (type == 'coupon_type') {
                        var name = 'coupon_name'
                        var id = 'coupon_type_id'
                        var selected_id_array = temp_coupon_type_id_array
                    } else {
                        var name = 'giftvoucher_name'
                        var id = 'gift_voucher_id'
                        var selected_id_array = temp_gift_voucher_id_array
                    }
                    $("#list").html(tpl('tpl_list', {
                        data: data.data,
                        name: name,
                        id: id,
                        selected_id_array: selected_id_array
                    }))
                    utilAdmin.page(".M-box3", data['total_count'], data["page_count"], page_index, LoadingInfo);
                }
            });
        }

        // 搜索
        $('.search').on('click', function () {
            LoadingInfo(1)
        })
    })
</script>